﻿using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using AdTech.Common.WPF;
using AdTech.Snap.Interop;

namespace AdTech.Snap
{
    #pragma warning disable 1591
    public class DropboxUIDExtractorViewModel : GlassViewModel
    {
        // Fields
        private readonly IDropboxFactory dropboxFactory;
        private int _DropboxUID;
        private DelegateCommand _SaveCommand;
        private DelegateCommand<string> _ExtractUIDCommand;
        private DelegateCommand<RoutedEventArgs> _LoadedCommand;
        private DelegateCommand<CancelEventArgs> _ClosingCommand;
        public const string HelpText =
            "With the help of this dialog, Snap can easily find your UserID with your input.\r\n" +
            "Just copy a public link of any file inside Dropbox to the clipboard via the Dropbox " +
            "section within the file's contextmenu. Snap will listen to the clipboard and use that " +
            "link to extract your UserID.\r\nOnce you're finished, hit the OK button to take the extracted " +
            "UserID into the configuration dialog.";

        // Properties
        public int DropboxUID
        {
            get { return _DropboxUID; }
            set { if (value == _DropboxUID) return; _DropboxUID = value; NotifyPropertyChanged(() => this.DropboxUID); }
        }
        #region Commands
        public ICommand SaveCommand
        {
            get
            {
                if (_SaveCommand == null)
                    _SaveCommand = new DelegateCommand(OnExecuteSave, OnCanExecuteSave);
                return _SaveCommand;
            }
        }
        public ICommand ExtractUIDCommand
        {
            get
            {
                if (_ExtractUIDCommand == null)
                    _ExtractUIDCommand = new DelegateCommand<string>(OnExecuteExtractUID);
                return _ExtractUIDCommand;
            }
        }
        public ICommand LoadedCommand
        {
            get
            {
                if (_LoadedCommand == null)
                    _LoadedCommand = new DelegateCommand<RoutedEventArgs>(OnExecuteLoaded);
                return _LoadedCommand;
            }
        }
        public ICommand ClosingCommand
        {
            get
            {
                if (_ClosingCommand == null)
                    _ClosingCommand = new DelegateCommand<CancelEventArgs>(OnExecuteClosing);
                return _ClosingCommand;
            }
        }
        #endregion

        public DropboxUIDExtractorViewModel(IDropboxFactory dropboxFactory)
        {
            this.dropboxFactory = dropboxFactory;
        }

        #region Commands
        protected virtual bool OnCanExecuteSave()
        {
            return this.DropboxUID > 0;
        }

        protected virtual void OnExecuteSave()
        {
            if (this.CloseCommand.CanExecute(null))
            {
                this.DialogResult = true;
                this.CloseCommand.Execute(null);
            }
        }

        protected virtual void OnExecuteExtractUID(string source)
        {
            if (!String.IsNullOrEmpty(source))
            {
                this.DropboxUID = this.dropboxFactory.ExtractUserID(source);
            }
        }

        [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
        protected virtual void OnExecuteLoaded(RoutedEventArgs e)
        {
            Window wnd = e.Source as Window;
            if (wnd == null)
                return;

            // Extend Aero Frame
            IntPtr windowHandle = new WindowInteropHelper(wnd).Handle;
            Thickness margin = new Thickness(0);
            FrameworkElement extendedArea = wnd.FindName("border") as FrameworkElement;
            if (extendedArea != null)
                margin.Bottom += extendedArea.ActualHeight;
            this.ExtendWindowFrame(windowHandle, margin);

            // Add Clipboard listener
            ClipboardHelper.RegisterClipboardHandler(windowHandle, ProcessClipboardForDropboxLink);
        }

        [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
        protected virtual void OnExecuteClosing(CancelEventArgs e)
        {
            e.Cancel = !ClipboardHelper.UnregisterClipboardHandler();
        }
        #endregion

        protected virtual void ProcessClipboardForDropboxLink()
        {
            if (Clipboard.ContainsText(TextDataFormat.Text))
            {
                string text = Clipboard.GetText(TextDataFormat.Text).Trim();
                if (this.ExtractUIDCommand.CanExecute(text))
                    this.ExtractUIDCommand.Execute(text);
            }
        }
    }
    #pragma warning restore 1591
}
